package com.yun.blog.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yun.blog.bean.Result;
import com.yun.blog.bean.Tag;
import com.yun.blog.service.TagService;
import com.yun.blog.utils.RandomColor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


/**
 * title        : TagController
 * projectName  : dantouBlog-back
 * description  : TODO
 * author       : simple
 * date         : 2022/11/26下午2:48
 */

@Slf4j
@RestController
@RequestMapping("/tag")
public class TagController {

    @Resource
    TagService tagService;


    @PostMapping("/saveTag")
    public Result saveTag(@RequestBody Tag tag) {
        // 先判断是否存在
        if (tagService.getByTagName(tag.getTagName()) != null) {
            return Result.fail("标签已存在");
        }

        // 如果没传递颜色或者颜色为空
        if (!StringUtils.hasText(tag.getTagColor())) {
            tag.setTagColor(RandomColor.RandomHex());
        }

        try {
            tagService.save(tag);
        } catch (Exception e) {
            log.error("保存标签失败：错误信息为：{}", e.getMessage());
            return Result.fail(e.getMessage());
        }
        return Result.success("保存成功！");
    }

    @GetMapping("/deleteTag")
    public Result deleteTag(Integer tagId) {
        try {
            tagService.removeById(tagId);
        } catch (Exception e) {
            log.error("保存删除失败：错误信息为：{}", e.getMessage());
            return Result.fail(e.getMessage());
        }
        return Result.success("删除成功！");
    }

    @PostMapping("/updateTag")
    public Result updateTag(@RequestBody Tag tag) {
        // 先判断标签名称是否存在
        Tag tag1 = tagService.getByTagName(tag.getTagName());
        if (tag1 != null && !tag1.getTagId().equals(tag.getTagId())) {
            return Result.fail("标签名称已存在");
        }

        // 如果没传递颜色或者颜色为空
        if (!StringUtils.hasText(tag.getTagColor())) {
            tag.setTagColor(RandomColor.RandomHex());
        }

        try {
            tagService.updateById(tag);
        } catch (Exception e) {
            log.error("修改标签失败：错误信息为：{}", e.getMessage());
            return Result.fail(e.getMessage());
        }
        return Result.success("修改成功！");
    }

    @GetMapping("/getTags")
    public Result getTags(@RequestParam(defaultValue = "10") Integer pageSize,
                          @RequestParam(defaultValue = "1") Integer pageNum,
                          @RequestParam(defaultValue = "") String searchText) {
        log.info("pageSize:{}", pageSize);
        log.info("pageNum:{}", pageNum);
        log.info("searchText:{}", searchText);
        Result success = Result.success();
        LambdaQueryWrapper<Tag> queryWrapper = Wrappers.lambdaQuery();
        // 如果有查询条件就查询名称一样的
        if (StringUtils.hasText(searchText)) {
            queryWrapper.like(Tag::getTagName, searchText);
        }
        Page<Tag> page = tagService.page(new Page<>(pageNum, pageSize), queryWrapper);

        success.add("page", page);
        return success;
    }

    /**
     * 获取全部标签
     *
     * @return java.util.List<com.yun.blog.bean.Category>
     **/
    @GetMapping("/getAllTags")
    public List<Tag> getAllTags() {
        try {
            return tagService.list();
        } catch (Exception e) {
            log.error("获取全部标签出错，错误信息为：{}", e.getMessage());
            return null;
        }
    }

    /**
     * 获取全部可用标签
     *
     * @return java.util.List<com.yun.blog.bean.Category>
     **/
    @GetMapping("/getAllUseTags")
    public List<Tag> getAllUseTags() {
        try {
            return tagService.listByIsUse(true);
        } catch (Exception e) {
            log.error("获取全部标签出错，错误信息为：{}", e.getMessage());
            return null;
        }
    }

    @GetMapping("/getTagById")
    public Tag getTagById(Integer tagId) {

        try {
            return tagService.getByTagId(tagId);
        } catch (Exception e) {
            log.error("获取标签出错，错误信息为：{}", e.getMessage());
            return null;
        }
    }

}
